import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "../stores/user.store";
import { useSysConfig } from "@/configs/sys.config";
import adminRoutes from "@/router/routes/admin.routes";
import indexRoutes from "@/router/routes/index.routes";
import h5Routes from "@/router/routes/h5.routes";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // 根据useSysConfig().project取对应的路由表
  routes: {
    default: [],
    admin: adminRoutes,
    index: indexRoutes,
    h5: h5Routes
  }[useSysConfig().project]
});

// 路由白名单，不参与鉴权
export const routeWhiteUrl = ["/sys/login"];

router.beforeEach((to, from, next) => {
  if (useUserStore().isLogin) {
    if (routeWhiteUrl.includes(to.path)) {
      return next("/");
    }
  } else {
    if (!routeWhiteUrl.includes(to.path)) {
      return next("/sys/login");
    }
  }
  next();
});

export default router;
